package com.ocr.dao;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;




import com.ocr.entities.HistoryItem;
import com.ocr.entities.SettingItem;
import com.ocr.utils.ApplicationConstant;

import android.content.Context;
import android.util.Log;

public class SettingDao {
	private Context context;

	public SettingDao(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
	}
	public void saveSetting(SettingItem settingItem){
		deleteAll();
		add(settingItem);
	}
	public Boolean add(SettingItem settingItem) {
		try {
			FileOutputStream fo = context.openFileOutput(ApplicationConstant.SETTING_FILE,
					Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo = new BufferedOutputStream(fo);
			ObjectOutputStream obo = new ObjectOutputStream(bo);
			obo.writeObject(settingItem);
			obo.close();
			bo.close();
			fo.close();
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	public ArrayList<SettingItem> loadAll() {
		ArrayList<SettingItem> listItems = new ArrayList<SettingItem>();
		try {
			FileInputStream fi = context.openFileInput(ApplicationConstant.SETTING_FILE);
			BufferedInputStream bi = new BufferedInputStream(fi);
			ObjectInputStream oi = new ObjectInputStream(bi);
			SettingItem item;
			while (bi.available() > 0) {
				item = (SettingItem) oi.readObject();
				if (item != null)
					listItems.add(item);
			}
			oi.close();
			bi.close();
			fi.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return listItems;
	}
	public Boolean deleteAll(){
		FileOutputStream fo;
		try {
			fo = context.openFileOutput(ApplicationConstant.SETTING_FILE,Context.MODE_WORLD_WRITEABLE);
			BufferedOutputStream bo=new BufferedOutputStream(fo);
	   		ObjectOutputStream obo=new ObjectOutputStream(bo);
	   		obo.close();
	        bo.close();
	   		return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	
}
